<template>
  <div>
    <BasicTable @register="registerTable" :rowSelection="rowSelection">
      <template #toolbar>
       <a-button type="primary" @click="handleAdd()" v-auth="'ACTION_appUser_add'">新增用户</a-button>
<!--        <a-button type="primary" @click="handleImport()" v-auth="'ACTION_appUserImport'">新会员导入</a-button>-->
<!--        <a-button type="primary" @click="handleUnBindOpenId(selectedRow)" :disabled="!hasSelectedRows" v-auth="'ACTION_unbindWeixin'">解绑微信 </a-button>-->
<!--        <a-button type="primary" @click="handleChangeMobile(selectedRow)" :disabled="!hasSelectedRows" v-auth="'ACTION_changeMobile'">换绑手机 </a-button>-->
<!--        <a-button type="primary" @click="handleResetPassword(selectedRow)" :disabled="!hasSelectedRows" v-auth="'ACTION_resetPassword'">重置密码 </a-button>-->
        <a-button type="primary" @click="handleOpenBalanceLogDrawer(selectedRow)" :disabled="!hasSelectedRows" v-auth="'ACTION_appUser_balance'">账户余额管理 </a-button>
<!--        <a-button type="primary" @click="handleUserOrder(selectedRow)" :disabled="!hasSelectedRows">我的订单 </a-button>-->
<!--        <a-button type="primary" @click="handleUserOptLog(selectedRow)" :disabled="!hasSelectedRows" v-auth="'ACTION_userOptLog'">操作日志 </a-button>-->
      </template>
      <template #action="{ record }">
        <TableAction :actions="getTableAction(record)" :dropDownActions="getDropDownAction(record)" />
      </template>
    </BasicTable>
    <UserEditDrawer @register="registerUserEditDrawer" @success="handleSuccess" />
    <UserSignDrawer @register="registerUserSignDrawer" />
    <BalanceLogDrawer @register="registerBalanceLogDrawer" />
    <UserOrderDrawer @register="registerUserOrderDrawer" />
<!--    <UserOptLogModal @register="registerUserOptLogModal" />-->
  </div>
</template>
<script lang="ts" setup name="appUser">
import { ActionItem, BasicTable, TableAction } from '@apps/antd-admin/src/components/Table';
import { useDrawer } from '@apps/antd-admin/src/components/drawer';
import { useBaseTable } from '@apps/antd-admin/src/hooks';
import { columns, searchFormSchema } from './data/data';
import { Api, resetPwd, unBindOpenId } from './data/api';
import { useModal } from '@apps/antd-admin/src/components/modal';
import { useMessage } from '@apps/antd-admin/src/hooks/web/useMessage';
import UserEditDrawer from './components/UserEditDrawer.vue';
import UserSignDrawer from './components/UserSignDrawer.vue';
import BalanceLogDrawer from './components/BalanceLogDrawer.vue';
import UserOrderDrawer from './components/UserOrderDrawer.vue';
import UserOptLogModal from '../userOptLog/UserOptLogModal.vue';

const { prefixCls, tableContext, handleDelete, handleSuccess, handlePost, handleBatchDelete } = useBaseTable({
  designScope: 'appUser-template',
  tableProps: {
    //表格标题
    title: '机构账号列表',
    //主键字段
    rowKey: 'userId',
    //列表查询handler
    handlerName: 'appUserInfoListHandler',
    //列表接口
    listUrl: Api.page,
    //删除接口
    delUrl: Api.remove,
    //列定义
    columns,
    //查询表单配置
    formConfig: {
      labelWidth: 120,
      showExportButton: true,
      exportButtonOptions: {
        code: 'ACTION_appUserExport',
      },
      exportStartDateLabel: 'beginDate',
      exportEndDateLabel: 'endDate',
      schemas: searchFormSchema,
    },
    //默认排序(排序字段上需设置defaultSortOrder:'descend')
    defSort: {
      order: 'user.createTime',
      sort: 'desc',
    },
    //操作列配置
    actionColumn: {
      width: 85,
      title: '操作',
      dataIndex: 'action',
      slots: { customRender: 'action' },
      fixed: 'right',
    },

    rowSelection: {
      type: 'radio',
    },
  },
});
const [registerTable, { reload }, { rowSelection, hasSelectedRows, selectedRow }] = tableContext;
const [registerUserEditDrawer, { openDrawer: openUserEditDrawer }] = useDrawer();
const [registerUserSignDrawer, { openDrawer: openUserSignDrawer }] = useDrawer();
const [registerBalanceLogDrawer, { openDrawer: openBalanceLogDrawer }] = useDrawer();
const [registerUserOrderDrawer, { openDrawer: openUserOrderDrawer }] = useDrawer();
const [registerUserOptLogModal, { openModal: openUserOptLogModal }] = useModal();
const { createConfirm } = useMessage();

/**
 * 行内操作栏
 */
function getTableAction(record): ActionItem[] {
  return [
   /* {
      label: '详情',
      onClick: handlerUserDetail.bind(null, record),
    },*/
    {
      label: '编辑',
      onClick: handleEdit.bind(null, record),
      auth: 'ACTION_appUser_edit',
    },
  ];
}

/**
 * 下拉操作栏
 */
function getDropDownAction(record): ActionItem[] {
  return [
    {
      label: '删除',
      auth: 'ACTION_appUser_del',
      popConfirm: {
        title: '是否确认删除',
        icon: 'ant-design:delete-outlined',
        confirm: handleDelete.bind(null, record),
      },
    },
   /* {
      label: '常用旅客',
      onClick: handleUserPassenger.bind(null, record),
      auth: 'ACTION_userPassenger',
    },
    {
      label: '常用联系人',
      onClick: handleUserContacts.bind(null, record),
      auth: 'ACTION_userContacts',
    },
    {
      label: '常用地址',
      onClick: handleUserAddress.bind(null, record),
      auth: 'ACTION_userAddress',
    },*/
  ];
}

function handleAdd() {
  openUserEditDrawer(true, {
    isUpdate: false,
  });
}
function handleEdit(record: Recordable) {
  openUserEditDrawer(true, {
    record,
    isUpdate: true,
  });
}

function handleOpenUserSignDrawer(record: Recordable) {
  openUserSignDrawer(true, {
    record,
    isUpdate: true,
  });
}

function handleOpenBalanceLogDrawer(record: Recordable) {
  openBalanceLogDrawer(true, {
    record,
    isUpdate: true,
  });
}


function handleUnBindOpenId(record: Recordable) {
  let userId = record.userId;
  createConfirm({
    iconType: 'info',
    title: '解绑微信',
    content: '确认解绑微信吗？',
    onOk: async () => {
      await unBindOpenId(userId);
      await reload();
    },
  });
}

function handleResetPassword(record: Recordable) {
  let userId = record.userId;
  createConfirm({
    iconType: 'info',
    title: '重置密码',
    content: '确认重置密码为888888吗？',
    onOk: async () => {
      await resetPwd(userId);
    },
  });
}


function handleUserOrder(record: Recordable) {
  openUserOrderDrawer(true, {
    record,
    isUpdate: false,
  });
}

function handleUserOptLog(record: Recordable) {
  record = { busType: 'appUser', userId: record.userId };
  openUserOptLogModal(true, {
    record,
    isUpdate: false,
  });
}


</script>
